$(function() {
	queryZone();
});

function queryZone() {
	var country = $('#country_query').val();
	var province = $('#province_query').val();
	var city = $('#city_query').val();
	$('#dg').datagrid({
		url : getHttpRequestPath("/statistics/queryZoneStatistics"),
		pageNumber : 1,
		queryParams : {
			country : country,
			province : province,
			city : city
		}
	});
	
	queryGraph(country, province, city);
}

function queryGraph(country, province, city) {
	var datas = [];
	$.post(getHttpRequestPath("/statistics/queryZoneGraph"), {
		country : country,
		province : province,
		city : city
	}, function(typesString) {
		JSON.parse(typesString, function(key, value) {
			var data = [];
			if (key != "") {
				data.push(key);
				data.push(value);
				datas.push(data);
			}
		});
		createGraph(datas);
	}).error(_error);
}

function createGraph(datas) {
	$('#container').highcharts({
        title: {
            text: '各地区订单占比'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: '各地区订单数占比',
            data: datas
        }]
    });
}

function formatZone(val, row, index) {
	var str = "";
	str += row.name;
	return str;
}

function formatOrders(val, row, index) {
	var str = "";
	str += row.value;
	return str;
}